package net.JwtDemo.service;


import lombok.RequiredArgsConstructor;
import net.JwtDemo.dto.LoginUser;
import net.JwtDemo.entity.UserInfo;
import net.JwtDemo.repository.UserInfoRepository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	UserInfoRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo user = userRepository
        		  .findByLoginName(username)
        		  .orElseThrow(() -> new UsernameNotFoundException("没找到用户"));        
    	
    	return new LoginUser(user);
    	
//    	 return org.springframework.security.core.userdetails.User.builder()
//    	            .username(user.getUsername())
//    	            .password(user.getPassword()) // 数据库已存储编码后的密码
//    	            .authorities(user.getRoles())
//    	            .build();
    }
}